package com.yuesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuesystem.dto.AreaOption;
import com.yuesystem.dto.requestbody.AreaRequestBody;
import com.yuesystem.dto.responsebody.AcademyNumberResponseBody;
import com.yuesystem.entity.Academy;
import com.yuesystem.entity.FixedItem;
import com.yuesystem.entity.Office;
import com.yuesystem.mapper.AcademyMapper;
import com.yuesystem.service.AcademyService;
import com.yuesystem.service.FixedItemService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 书院接口实现类.
 */

@Service
@Slf4j
public class AcademyServiceImpl extends
    ServiceImpl<AcademyMapper, Academy> implements AcademyService {
  @Autowired
  private AcademyMapper academyMapper;

  @Autowired
  private FixedItemService fixedItemService;
  
  /**
   * 增加书院.
   */
  @Override
  public boolean add(Academy academy) {
    List<Long> id = this.getByName(academy.getAcademyName());
    if (!id.isEmpty()) {
      return false;
    }
    this.save(academy);
    
    return true;
  }
  /**
   * 根据书院名字查询对应的ID号.
   *
   * @param name 书院名字
   * @return 书院条目id列表
   */
  @Override
  public List<Long> getByName(String name) {
    LambdaQueryWrapper<Academy> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.like(Academy::getAcademyName, name);
    List<Academy> academyList = this.list(queryWrapper);

    List<Long> list = academyList.stream().map((item) -> {
      return item.getId();
    }).collect(Collectors.toList());

    return list;
  }

  /**
   * 根据搜索框中的名称进行查询.
   *
   * @param name 搜索框内容
   * @return 书院类列表
   */
  @Override
  public List<Academy> search(String name) {
    LambdaQueryWrapper<Academy> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.like(Academy::getAcademyName, name);
    List<Academy> academyList = academyMapper.selectList(queryWrapper);
    return academyList;
  }


  /**
   * 获得中国七大地区分布.
   *
   * @return 地区分布响应类列表
   */
  @Override
  public List<AreaRequestBody> getByArea() {
    List<AreaRequestBody> areaBodyList = new ArrayList<>();
    List<String> areas = Arrays.asList("华北地区", "华东地区", "东北地区", "华中地区", "华南地区", "西南地区", "西北地区");
    String[][] provinces = {
        {"北京", "天津", "河北", "山西", "内蒙古"},
        {"上海", "江苏", "浙江", "山东", "安徽", "江西", "福建", "台湾"},
        {"辽宁", "吉林", "黑龙江"},
        {"湖北", "湖南", "河南"},
        {"广东", "广西", "海南", "香港", "澳门"},
        {"四川", "重庆", "贵州", "云南", "西藏"},
        {"陕西", "甘肃", "新疆", "青海", "宁夏"}
    };
    for (int i = 0; i < areas.size(); i++) {
      List<AreaOption> options = new ArrayList<>();
      String[] province = provinces[i];

      int j = 0;
      for (String p : province) {
        AreaOption option = new AreaOption();
        option.setChildren(new ArrayList<>());
        option.setProvince(p);
        options.add(option);
        j++;
      }

      AreaRequestBody areaBody = new AreaRequestBody();
      areaBody.setArea(areas.get(i));
      areaBody.setOptions(options);

      areaBodyList.add(areaBody);

    }
    return areaBodyList;
  }

  /**
   * 获得每个省的书院分页结果.
   *
   * @param page 起始页
   * @param pageSize 页数
   * @param province 省份
   * @return 书院类分页
   */
  @Override
  public Page<Academy> getPage(int page, int pageSize, String province) {
    Page<Academy> pageInfo = new Page<>(page, pageSize);

    LambdaQueryWrapper<FixedItem> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(FixedItem::getRelation, "位于");
    queryWrapper.likeRight(FixedItem::getObject, province);
    List<FixedItem> fixedItems = fixedItemService.list(queryWrapper);

    if (fixedItems.size() == 0) {
      return pageInfo;
    }

    // 获得书院id集合
    List<Long> ids = fixedItems.stream().map((item) -> {
      Long academyId = item.getAcademyId();
      return academyId;
    }).collect(Collectors.toList());

    //自动去重
    List<Long> listNew = new ArrayList<>(new TreeSet<>(ids));

    LambdaQueryWrapper<Academy> wrapper = new LambdaQueryWrapper<>();
    wrapper.in(Academy::getId, listNew);

    this.page(pageInfo, wrapper);
    return pageInfo;
  }

  /**
   * 获得各个省份书院的数量.
   *
   * @return 书院数量响应类列表
   */
  @Override
  public List<AcademyNumberResponseBody> getNumber() {
    List<AcademyNumberResponseBody> list = new ArrayList<>();
    String[] provinces = {
        "北京", "天津", "河北", "山西", "内蒙古",
        "上海", "江苏", "浙江", "山东", "安徽",
        "辽宁", "吉林", "黑龙江",
        "湖北", "湖南", "河南", "江西",
        "广东", "广西", "海南", "福建", "香港", "澳门",
        "四川", "重庆", "贵州", "云南", "西藏",
        "陕西", "甘肃", "新疆", "青海", "宁夏", "台湾"
    };

    for (String province : provinces) {
      AcademyNumberResponseBody body = new AcademyNumberResponseBody();
      body.setProvince(province);

      LambdaQueryWrapper<FixedItem> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.likeRight(FixedItem::getObject, province)
          .eq(FixedItem::getRelation, "位于");
      long count = fixedItemService.count(queryWrapper);
      body.setNumber((int) count);
      list.add(body);
    }

    return list;
  }

}
